use chrono::NaiveDateTime;
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};

use crate::entities::{prelude::ZaDemo, za_demo};

#[allow(dead_code)]
#[derive(Clone, Debug)]
pub struct DemoModel<'a> {
    conn: &'a sea_orm::DatabaseConnection,
}

#[allow(dead_code)]
impl<'a> DemoModel<'a> {
    pub fn new(conn: &'a sea_orm::DatabaseConnection) -> Self {
        DemoModel { conn }
    }
    pub async fn get_by_created(self, millis: i64) -> Result<Vec<za_demo::Model>, sea_orm::DbErr> {
        let t = NaiveDateTime::from_timestamp_millis(millis);
        ZaDemo::find()
            .filter(za_demo::Column::CreatedAt.lt(t))
            .all(self.conn)
            .await
    }

    pub async fn get_by_id(self, id: i64) -> Result<Option<za_demo::Model>, sea_orm::DbErr> {
        ZaDemo::find()
            .filter(za_demo::Column::Id.eq(id))
            .one(self.conn)
            .await
    }
}
